﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;

namespace h8
{
    public partial class Form1 : Form
    {
        private SQLiteConnection connection;
        private string currentEnglish;
        public Form1()
        {
            InitializeComponent();
            txtInput.KeyDown += txtInput_KeyDown;  // 绑定事件
            InitializeDatabase();
            LoadNextWord();
        }
        private void InitializeDatabase()
        {
            string dbPath = "vocabulary.db";
            bool dbExists = System.IO.File.Exists(dbPath);

            if (!dbExists)
            {
                SQLiteConnection.CreateFile(dbPath);
            }

            connection = new SQLiteConnection($"Data Source={dbPath};Version=3;");
            connection.Open();

            // 如果数据库是新建的，创建表和插入数据  
            if (!dbExists)
            {
                using (var cmd = new SQLiteCommand(
                    "CREATE TABLE IF NOT EXISTS Vocabulary (Id INTEGER PRIMARY KEY AUTOINCREMENT, English TEXT NOT NULL, Chinese TEXT NOT NULL);",
                    connection))
                {
                    cmd.ExecuteNonQuery();
                }

                try
                {
                    using (var cmd = new SQLiteCommand(
                        "INSERT INTO Vocabulary (English, Chinese) VALUES " +
                        "('apple', '苹果'), " +
                        "('computer', '计算机'), " +
                        "('hello', '你好'), " +
                        "('database', '数据库'), " +
                        "('programming', '编程');",
                        connection))
                    {
                        cmd.ExecuteNonQuery();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("插入数据失败：" + ex.Message);
                }
            }

            connection.Close();
        }

        private void LoadNextWord()
        {
            try
            {
                connection.Open();
                using (var cmd = new SQLiteCommand("SELECT English, Chinese FROM Vocabulary ORDER BY RANDOM() LIMIT 1", connection))
                {
                    using (var reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            currentEnglish = reader["English"].ToString();
                            lblChinese.Text = reader["Chinese"].ToString();
                        }
                        else
                        {
                            MessageBox.Show("数据库中没有单词！");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"数据库错误: {ex.Message}");
            }
            finally
            {
                connection.Close();
                txtInput.Clear();
                txtInput.Focus();
            }
        }

        private void CheckAnswer()
        {
            if (txtInput.Text.Trim().Equals(currentEnglish, StringComparison.OrdinalIgnoreCase))
            {
                lblResult.Text = "正确！";
                lblResult.ForeColor = System.Drawing.Color.Green;
                LoadNextWord();
            }
            else
            {
                lblResult.Text = $"错误！正确答案：{currentEnglish}";
                lblResult.ForeColor = System.Drawing.Color.Red;
            }
        }

        // 窗体控件事件
        private void txtInput_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                CheckAnswer();
            }
        }
    }
}
